package cn.gson.springboot.model.service;


import cn.gson.springboot.model.dao.QaDao;
import cn.gson.springboot.model.mapper.QaMapper;
import cn.gson.springboot.model.pojos.Qa;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class QaService {
    @Autowired
    QaDao qaDao;
    @Autowired
    QaMapper qaMapper;

    //新增
    public void addQa(Qa qa){
        qaDao.save(qa);//一次保存一个对象
    }

    //删除
    public void deleteOne(int qaId){
        qaDao.deleteById(qaId);
    }

    //查找所有
    public List<Qa> selectAll(String search, Integer type) {
        return (List<Qa>)qaMapper.selectAll(search,type);
    }


    //根据id修改
    public void alter(int qaId,Qa qa){
         if ( qaDao.findById(qaId)!=null){//找到该对象
             Qa qa1=qaDao.findById(qaId).get();
            qa1.setQaAnswer(qa.getQaAnswer());
            qa1.setQaQuestion(qa.getQaQuestion());
            qaDao.save(qa1);
         }
    }


}
